﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsuranceSell.Code.Entities;
using InsuranceSell.Code.Services;
using InsuranceSell.Code.Data;

namespace InsuranceSell
{
    public partial class frmAccountManager : Form
    {
        bool isUpdate = false;
        bool isAdduser = false;
        public frmAccountManager()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (isAdduser || !isUpdate)
            {
                #region Add New User
                User user = new User(); user.FullName = txtFullName.Text.Trim();
                user.Username = txtUserName.Text.Trim();
                user.Password = Encryptor.HashMD5(txtPassword.Text.Trim());
                user.isAdmin = chkisAdmin.Checked;
                user.Email = txtEmail.Text.Trim();
                user.Telephone = txtTelephone.Text.Trim();
                user.Mobile = txtMobile.Text.Trim();

                bool isAdded = UserServices.AddNewUser(user);

                if (isAdded == true)
                {
                    MessageBox.Show("Đã thêm thành công!", "Thêm user");
                    DataTable allUsers = UserServices.LoadAllUsers();

                    if (allUsers != null)
                    {

                        dtgAccount.DataSource = allUsers;
                        allUsers.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("User này đã tồn tại!");
                    DataTable allUsers = UserServices.LoadAllUsers();

                    if (allUsers != null)
                    {

                        dtgAccount.DataSource = allUsers;
                        allUsers.Dispose();
                    }
                }
                #endregion
                isAdduser = false;
            }
            else {
                #region Edit User
                User user = new User(); user.FullName = txtFullName.Text.Trim();
                user.Username = txtUserName.Text.Trim();
                user.Password = Encryptor.HashMD5(txtPassword.Text.Trim());
                user.Email = txtEmail.Text.Trim();
                user.Telephone = txtTelephone.Text.Trim();
                user.Mobile = txtMobile.Text.Trim();

                bool isUpdated = UserServices.UpdateUser(user);

                if (isUpdated == true)
                {
                    MessageBox.Show("Đã sửa thành công!", "Sửa thông tin user");
                    DataTable allUsers = UserServices.LoadAllUsers();

                    if (allUsers != null)
                    {

                        dtgAccount.DataSource = allUsers;
                        allUsers.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("Sửa thông tin không thành công!");
                    DataTable allUsers = UserServices.LoadAllUsers();

                    if (allUsers != null)
                    {

                        dtgAccount.DataSource = allUsers;
                        allUsers.Dispose();
                    }
                }
                #endregion
            }
        }

        private void frmAccountManager_Load(object sender, EventArgs e)
        {
            DataTable allUsers = UserServices.LoadAllUsers();

            if (allUsers != null)
            {

                dtgAccount.DataSource = allUsers;
                allUsers.Dispose();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa Tài khoản này không ?", "Xác nhận xóa tài khoản", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                User user = new User();
                user.Username = txtUserName.Text.Trim();

                bool isDeleted = UserServices.DeleteUser(user);

                if (isDeleted)
                {
                    MessageBox.Show("Đã xóa Tài Khoản: \"" + user.Username + "\" thành công !");
                    DataTable allUsers = UserServices.LoadAllUsers();

                    if (allUsers != null)
                    {

                        dtgAccount.DataSource = allUsers;
                        allUsers.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("Lỗi : không thể xóa Tài Khoản: \"" + user.Username + "\" !");
                    DataTable allUsers = UserServices.LoadAllUsers();

                    if (allUsers != null)
                    {

                        dtgAccount.DataSource = allUsers;
                        allUsers.Dispose();
                    }
                }
            }
        }

        private void dtgAccount_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                txtUserName.Text = dtgAccount.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtFullName.Text = dtgAccount.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtMobile.Text = dtgAccount.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtTelephone.Text = dtgAccount.Rows[e.RowIndex].Cells[4].Value.ToString();
                txtEmail.Text = dtgAccount.Rows[e.RowIndex].Cells[5].Value.ToString();
                chkisAdmin.Checked = bool.Parse(dtgAccount.Rows[e.RowIndex].Cells[1].Value.ToString());
                isUpdate = true;
                isAdduser = false;
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            isUpdate = true;
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            isAdduser = true;
        }
    }
}
